﻿using System;
using System.Collections;
using System.Collections.Specialized;

namespace DbManager.Mapping
{
    public class MappingSchema
    {

        #region Public Members

        #endregion

        private readonly Hashtable _mappers = new Hashtable();
        private readonly ListDictionary _pendingMappers = new ListDictionary();
        public ObjectMapper GetObjectMapper(Type type)
        {
            ObjectMapper om = (ObjectMapper)_mappers[type];

            if (om == null)
            {
                lock (_mappers.SyncRoot)
                {
                    om = (ObjectMapper)_mappers[type];

                //    // This object mapper is initializing right now.
                //    // Note that only one thread can access to _pendingMappers each time.
                //    //
                    if (om == null)
                        om = (ObjectMapper)_pendingMappers[type];

                //    if (om == null)
                //    {
                //        om = CreateObjectMapper(type);

                //        if (om == null)
                //            throw new MappingException(
                //                string.Format("Cannot create object mapper for the '{0}' type.", type.FullName));

                //        _pendingMappers[type] = om;

                //        try
                //        {
                //            om.Init(this, type);
                //        }
                //        finally
                //        {
                //            _pendingMappers.Remove(type);
                //        }

                //        // Officially publish this ready to use object mapper.
                //        //
                //        SetObjectMapperInternal(type, om);
                //    }
                }
            }

            return om;
        }
    }
}
